package cn.mastor.org.controller;

import cn.mastor.basic.anno.PreAuthorize;
import cn.mastor.basic.util.PageList;
import cn.mastor.org.domain.Department;
import cn.mastor.org.query.DepartmentQuery;
import cn.mastor.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@CrossOrigin
@RequestMapping("/dept")
@RestController
@PreAuthorize
public class DepertmentController {
    @Autowired
    private IDepartmentService service;
    @PostMapping("/tree")
    public List<Department> deptTree() {
        return service.deptTree();
    }
    @PostMapping
    public PageList<Department> findbyPage(@RequestBody DepartmentQuery query){
        return service.queryByPage(query);
    }

    @RequestMapping("/aa")
    @PreAuthorize(name = "分页查询部门",sn = "department:queryByPage")
    public PageList findByid(Long id){
        List<Department> departments = service.LowDeper(id);
        Department byId = service.findById(id);
        ArrayList<Object> list = new ArrayList<>();
        list.add(byId);
        PageList pageList = new PageList(1, departments);
        return pageList;
    }
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询部门",sn = "department:findById")
    public Department findById(@PathVariable("id") Long id) {
        return service.findById(id);
    }
    @GetMapping()
    public List findAll() {
        return  service.findAll();
    }
    @PutMapping
    public void addOrUpdate(@RequestBody Department department){
        if (null == department.getId()){
            // 添加
            service.add(department);
        }else {
            // 修改
            service.update(department);
        }
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        service.delete(id);
    }
    @PatchMapping()
    @PreAuthorize(name = "批量删除部门",sn = "department:batchDel")
    public void patchdel(@RequestBody List<Long> ids) {
        service.batchDel(ids);
    }

}
